package userInterface;
import java.awt.GridLayout;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Auction;
import model.Bidder;
import model.Control;
import model.Item;

/**
 * @author fahad
 * @author Daniel Bartholomew
 */
@SuppressWarnings("serial")
public class ItemView extends JFrame
	{
	/**
	 * The auction text field is use in the button to check if auction text field is empty or not.
	 * When we submit the form it is important to have all the information before submitting. 
	 */
	private JTextField item_name_textbar;

	/**
	 * The contact text field is use in the button to check if contact text field is empty or not.
	 * When we submit the form it is important to have all the information before submitting. 
	 */
	private JTextField minimum_starting_bid;

	/**
	 * The auction text field is use in the button to check if auction text field is empty or not.
	 * When we submit the form it is important to have all the information before submitting. 
	 */
	private JTextField current_bid_textfield;
	private JTextField bid_textfield;
	private JButton btnBack = null;
	private JButton BidButton = null;
	
	private Control my_control;

	/**
	 * 
	 */
	private static Item my_item;
	
	private static Auction my_auction;
	
	/**
	 * Constructor to display a GUI for auction forms.
	 * Constructor
	 */
	public ItemView(Item the_Item, Control control, Auction auction)
	{
		super("item View");
		my_control = control;
		my_auction = auction;
		this.setSize(640, 480);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(false);
		my_item = the_Item;
		item();

	}
	
	public void item()
	{
		//Item help = new Item();
		setLayout(new GridLayout(15,1));

		JPanel item_name = new JPanel();
		JLabel item_name_display = new JLabel("Item Name-----------------------------------:");
		item_name.add(item_name_display);
		item_name_textbar = new JTextField(30);
		item_name_textbar.setEditable(false);
		item_name.add(item_name_textbar);
		item_name_textbar.setText(my_item.getName());
		
		JPanel minimun_starting_bid = new JPanel();
		JLabel minimun_starting_bid_display = new JLabel("Minimum Starting bid-----------------:");
		minimun_starting_bid.add(minimun_starting_bid_display);
		minimum_starting_bid = new JTextField(30);
		minimum_starting_bid.setEditable(false);
		minimun_starting_bid.add(minimum_starting_bid);
		minimum_starting_bid.setText(my_item.getMinPrice()+"");
		
		add(getBtnBack());
		add(item_name);
		add(minimun_starting_bid);
		
		if (my_control.getActiveUser() instanceof Bidder) {
			Double bid = ((Bidder) my_control.getActiveUser()).getBids().get(my_item);
			
			JPanel current_bid_items = new JPanel();
			current_bid_items.add(new JLabel("Your current bid: "));	
			current_bid_textfield = new JTextField(30);
			current_bid_textfield.setEditable(false);
			current_bid_items.add(current_bid_textfield);
			if (bid != null) {
				current_bid_textfield.setText(bid.toString());
			} else {
				current_bid_textfield.setText("None");
			}
			add(current_bid_items);
			
			JPanel display_bidder_items = new JPanel();
			display_bidder_items.add(getBidButton());	
			bid_textfield = new JTextField(30);
			display_bidder_items.add(bid_textfield);
			add(display_bidder_items);
		}
	}
	
	/**
	 * This method initializes btnBack	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getBtnBack() {
		if (btnBack == null) {
			btnBack = new JButton("Back");
			btnBack.setBounds(new Rectangle(495, 404, 106, 31));
			btnBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (my_auction != null) {
						ItemListView ilv = new ItemListView(my_control, my_auction);
						ilv.setLocationRelativeTo(null);
						ilv.setVisible(true);
						dispose();
					} else {
						BidListView blv = new BidListView(my_control);
						blv.setLocationRelativeTo(null);
						blv.setVisible(true);
						dispose();
					}
				}
			});
		}
		return btnBack;
	}

	private JButton getBidButton() {
		if (BidButton == null) {
			BidButton = new JButton("Bid:");
			//BidButton.setBounds(new Rectangle(495, 404, 106, 31));
			BidButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Bidder bidder = (Bidder) my_control.getActiveUser();
					double bidprice = -1;
					try {
						bidprice = Double.parseDouble(bid_textfield.getText());
					} catch (NumberFormatException e1) {
						JOptionPane.showMessageDialog(null,	"Bad Bid Price");
						return;
					}
					boolean accepted = bidder.newBid(my_item, bidprice);
					my_control.saveBids(bidder);
					if (accepted) {
						BidListView blv = new BidListView(my_control);
						blv.setLocationRelativeTo(null);
						blv.setVisible(true);
						dispose();
					} else {
						JOptionPane.showMessageDialog(null,	"Bid Rejected");
					}
				}
			});
		}
		return BidButton;
	}
}